package org.bits.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.*;

import org.bits.data.*;
import org.bits.model.DataAccessModel;
import org.jutils.ui.model.ItemComboBoxModel;

/*******************************************************************************
 * 
 ******************************************************************************/
public class VersionDialog
{
    /**  */
    private OkCancelDialog dialog;
    /**  */
    private ItemComboBoxModel<Product> productModel;
    /**  */
    private ItemComboBoxModel<Release> releaseModel;
    /**  */
    private ItemComboBoxModel<String> buildModel;

    /**  */
    private DataAccessModel dataModel;

    /***************************************************************************
     * @param owner
     * @param title
     **************************************************************************/
    public VersionDialog( DataAccessModel dataModel, List<Product> products,
        Window owner, String title )
    {
        this.dataModel = dataModel;

        this.productModel = new ItemComboBoxModel<Product>();
        this.releaseModel = new ItemComboBoxModel<Release>();
        this.buildModel = new ItemComboBoxModel<String>();

        this.dialog = new OkCancelDialog( owner, title, createVersionPanel() );

        this.productModel.setItems( products );
        this.dialog.setOkButtonEnabled( false );
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    private Component createVersionPanel()
    {
        JPanel panel = new JPanel( new GridBagLayout() );
        JComboBox productsComboBox = new JComboBox( productModel );
        JComboBox releasesComboBox = new JComboBox( releaseModel );
        JComboBox buildsComboBox = new JComboBox( buildModel );
        GridBagConstraints constraints;

        productsComboBox.addActionListener( new ProductChosenListener() );
        releasesComboBox.addActionListener( new ReleaseChosenListener() );
        buildsComboBox.addActionListener( new BuildChosenListener() );

        constraints = new GridBagConstraints( 0, 0, 1, 1, 1.0, 1.0,
            GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
            new Insets( 10, 10, 2, 10 ), 0, 0 );
        panel.add( new JLabel( "Product" ), constraints );

        constraints = new GridBagConstraints( 0, 1, 1, 1, 1.0, 1.0,
            GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
            new Insets( 2, 10, 10, 10 ), 0, 0 );
        panel.add( productsComboBox, constraints );

        constraints = new GridBagConstraints( 0, 2, 1, 1, 1.0, 1.0,
            GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
            new Insets( 10, 10, 2, 10 ), 0, 0 );
        panel.add( new JLabel( "Release" ), constraints );

        constraints = new GridBagConstraints( 0, 3, 1, 1, 1.0, 1.0,
            GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
            new Insets( 2, 10, 10, 10 ), 0, 0 );
        panel.add( releasesComboBox, constraints );

        constraints = new GridBagConstraints( 0, 4, 1, 1, 1.0, 1.0,
            GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
            new Insets( 10, 10, 2, 10 ), 0, 0 );
        panel.add( new JLabel( "Build" ), constraints );

        constraints = new GridBagConstraints( 0, 5, 1, 1, 1.0, 1.0,
            GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
            new Insets( 2, 10, 10, 10 ), 0, 0 );
        panel.add( buildsComboBox, constraints );

        return panel;
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    public Version showVersionDialog()
    {
        Version v = null;

        if( dialog.showDialogView() )
        {
            v = new Version();

            v.product = productModel.getSelectedItem();
            v.release = releaseModel.getSelectedItem();
            v.build = buildModel.getSelectedItem();
        }

        return v;
    }

    /***************************************************************************
     * @param v
     **************************************************************************/
    public void setVersion( Version v )
    {
        productModel.setSelectedItem( v.product );
        fillReleases( v.product );

        releaseModel.setSelectedItem( v.release );
        fillBuilds( v.release );

        buildModel.setSelectedItem( v.build );
    }

    private void fillReleases( Product p )
    {
        // releaseModel.clearItems();
        releaseModel.setItems( dataModel.getReleases( p ) );
        buildModel.clear();
    }

    private void fillBuilds( Release r )
    {
        // buildModel.clearItems();
        buildModel.setItems( dataModel.getBuilds( r ) );
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class ProductChosenListener implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent e )
        {
            Product p = productModel.getSelectedItem();

            if( p != null )
            {
                fillReleases( p );
            }

            dialog.setOkButtonEnabled( false );
        }
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class ReleaseChosenListener implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent e )
        {
            Release r = releaseModel.getSelectedItem();

            if( r != null )
            {
                fillBuilds( r );
            }

            dialog.setOkButtonEnabled( false );
        }
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class BuildChosenListener implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent e )
        {
            String b = buildModel.getSelectedItem();

            if( b != null )
            {
                dialog.setOkButtonEnabled( true );
            }
        }
    }
}
